package pl.edu.agh.experts.diseases.gui;

import java.awt.Container;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import org.drools.KnowledgeBase;

import pl.edu.agh.experts.diseases.DiseaseHolder;
import pl.edu.agh.experts.diseases.Printer;
import pl.edu.agh.experts.diseases.QuestionProvider;
import pl.edu.agh.experts.diseases.commons.Decision;
import pl.edu.agh.experts.diseases.commons.MetaKnowledge;
import pl.edu.agh.experts.diseases.commons.Symptom;

public class MainWindow extends JFrame {

	private static MainWindow instance;

	private JLabel questionLabel;
	private JComboBox comboBox;
	private JTextField textField;
	private JButton okButton;
	private JTextArea textArea;
	private QuestionType questionType;
	private KnowledgeBase kbaseResults;
	private QuestionProvider questionProvider;
	private MetaKnowledge metaKnowledge;
	private Symptom symptom;

	private Container createQuestionLayout() {
		Container container = new Container();
		container.setLayout(new GridLayout(6, 2));

		questionLabel = new JLabel();
		comboBox = new JComboBox();
		textField = new JTextField();

		container.add(questionLabel);
		container.add(comboBox);

		container.add(new JLabel());
		container.add(new JLabel());

		container.add(new JLabel("Jeśli odpowiedzialeś 'Nie jestem pewny', możesz podać wagę od 0 do 100 z jaką odczuwasz objaw"));

		container.add(textField);

		container.add(new JLabel());
		container.add(new JLabel());

		container.add(new JLabel());
		okButton = new JButton("OK");
		container.add(okButton);
		okButton.addActionListener(new MainWindowListener(this));

		return container;
	}

	public MainWindow() {
		super("Poradnia");
		instance = this;
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Container container = getContentPane();
		container.setLayout(new GridLayout(2, 1));
		add(createQuestionLayout());
		textArea = new JTextArea();
		add(textArea);
		textArea.setText(Printer.getResults(DiseaseHolder.getInstance().getAllDiseases()));

		pack();
		setVisible(true);

		Setup.getQuestionProvider().askPatient();
	}

	public void setMetaQuestion(MetaKnowledge metaKnowledge, KnowledgeBase kbaseResults, QuestionProvider questionProvider) {
		Printer.printMetaQuestion(metaKnowledge);
		this.kbaseResults = kbaseResults;
		this.questionProvider = questionProvider;
		this.metaKnowledge = metaKnowledge;
		questionType = QuestionType.MetaQuestion;
		questionLabel.setText(metaKnowledge.getQuestion());
		comboBox.removeAllItems();
		for (Decision decision : Decision.values()) {
			if (decision != Decision.Maybe) {
				if (decision == Decision.Yes)
					comboBox.addItem("Tak");
				else if (decision == Decision.No)
					comboBox.addItem("Nie");
			}
		}
		textField.setText("");
		textField.setEnabled(false);
		pack();
	}

	public void setQuestion(Symptom symptom, KnowledgeBase kbaseResults, QuestionProvider questionProvider) {
		Printer.printSymptomQuestion(symptom);
		this.kbaseResults = kbaseResults;
		this.questionProvider = questionProvider;
		this.symptom = symptom;
		questionType = QuestionType.SymptomQuestion;
		questionLabel.setText(symptom.getQuestion());
		comboBox.removeAllItems();
		for (Decision decision : Decision.values()) {
				if (decision == Decision.Yes)
					comboBox.addItem("Tak");
				else if (decision == Decision.No)
					comboBox.addItem("Nie");
				else if (decision == Decision.Maybe)
					comboBox.addItem("Nie wiem");
		}
		textField.setText("50");
		textField.setEnabled(true);
		pack();
	}

	public void endOfQuestions() {
		comboBox.setEnabled(false);
		textField.setEditable(false);
		textArea.setEditable(false);
		okButton.setEnabled(false);
	}

	public static MainWindow getInstance() {
		return instance;
	}

	public JLabel getQuestionLabel() {
		return questionLabel;
	}

	public JComboBox getComboBox() {
		return comboBox;
	}

	public JTextField getTextField() {
		return textField;
	}

	public JButton getOkButton() {
		return okButton;
	}

	public JTextArea getTextArea() {
		return textArea;
	}

	public QuestionType getQuestionType() {
		return questionType;
	}

	public KnowledgeBase getKbaseResults() {
		return kbaseResults;
	}

	public QuestionProvider getQuestionProvider() {
		return questionProvider;
	}

	public MetaKnowledge getMetaKnowledge() {
		return metaKnowledge;
	}

	public Symptom getSymptom() {
		return symptom;
	}
}
